/*
 * @lc app=leetcode.cn id=704 lang=javascript
 *
 * [704] 二分查找
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {
  let start = 0;
  let end = nums.length - 1;

  while (start <= end) {
    const middle = Math.floor((end + start) / 2);
    const middleValue = nums[middle];

    if (target === middleValue) {
      return middle;
    }

    if (target < middleValue) {
      end = middle - 1;
    }

    if (target > middleValue) {
      start = middle + 1;
    }
  }
  return -1;
};
// @lc code=end
